//
//  BVResetViewController.m
//  XiangYu
//
//  Created by YYKJ on 2017/6/5.
//  Copyright © 2017年 YYKJ. All rights reserved.
//  忘记密码

#import "BVResetPwdViewController.h"
#import "BVLoginAndRegisTF.h"
#import "BVLoginViewController.h"
#import "UIView+TransitionAnimation.h"
#import "BVCaptchaButton.h"
#import "BVMineTool.h"

@interface BVResetPwdViewController ()

@property (nonatomic ,strong) UITextField      *accountTF;
@property (nonatomic ,strong) UITextField      *verifyCode;
@property (nonatomic ,strong) UITextField      *pwdNew;
@property (nonatomic ,strong) UITextField      *confirmPwd;
@property (nonatomic ,strong) BVMineTool       *mineTool;
@property (nonatomic ,strong) BVCaptchaButton  *captchaBtn;

@end

@implementation BVResetPwdViewController

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.view endEditing:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.navigationItem setTitle:@"忘记密码"];
    [self layoutUI];
}

- (void)layoutUI{
    
    UIImageView *img = [[UIImageView alloc]init];
    [self.view addSubview:img];
    [img setImage:[UIImage imageNamed:@"login_image"]];
    img.sd_layout
    .topSpaceToView(self.view, 0)
    .centerXIs(ScreenW/2)
    .heightIs(160)
    .widthIs(320);
    
    self.accountTF = [self textFieldWithText:@"请输入您的手机号"];
    [self.view addSubview:self.accountTF];
    [self.accountTF setKeyboardType:UIKeyboardTypePhonePad];
    self.accountTF.sd_layout
    .leftSpaceToView(self.view, 20)
    .rightSpaceToView(self.view, 20)
    .topSpaceToView(img, 0)
    .heightIs(45);
    
    self.captchaBtn = [[BVCaptchaButton alloc]init];
    [self.view addSubview:self.captchaBtn];
    [self.captchaBtn setCountdown:10];
    [self.captchaBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    [self.captchaBtn.titleLabel setFont:[UIFont systemFontOfSize:16]];
    [self.captchaBtn setBackgroundColor:[UIColor BVBaseOrangeColor]];
    [self.captchaBtn.layer setCornerRadius:5.0];
    self.captchaBtn.sd_layout
    .topSpaceToView(self.accountTF, 10)
    .widthIs(100)
    .rightEqualToView(self.accountTF)
    .heightIs(45);
    
    self.verifyCode = [self textFieldWithText:@"请输入验证码"];
    [self.view addSubview:self.verifyCode];
    [self.verifyCode setKeyboardType:UIKeyboardTypePhonePad];
    self.verifyCode.sd_layout
    .leftSpaceToView(self.view, 20)
    .rightSpaceToView(self.captchaBtn, 0)
    .topSpaceToView(self.accountTF, 10)
    .heightIs(45);
    
    self.pwdNew = [self textFieldWithText:@"请输入新密码"];
    [self.view addSubview:self.pwdNew];
    self.pwdNew.sd_layout
    .leftSpaceToView(self.view, 20)
    .rightSpaceToView(self.view, 20)
    .topSpaceToView(self.verifyCode, 10)
    .heightIs(45);
    
    self.confirmPwd = [self textFieldWithText:@"请再次输入新密码"];
    [self.view addSubview:self.confirmPwd];
    self.confirmPwd.sd_layout
    .leftSpaceToView(self.view, 20)
    .rightSpaceToView(self.view, 20)
    .topSpaceToView(self.pwdNew, 10)
    .heightIs(45);
    
    UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:loginBtn];
    [loginBtn setTitle:@"确认修改" forState:UIControlStateNormal];
    [loginBtn setBackgroundColor:[UIColor BVBaseOrangeColor]];
    [loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [loginBtn.titleLabel setFont:[UIFont systemFontOfSize:16]];
    [loginBtn addTarget:self action:@selector(resetPersonalPwd) forControlEvents:UIControlEventTouchUpInside];
    [UIView makeCorner:5.0 view:loginBtn];
    loginBtn.sd_layout
    .leftSpaceToView(self.view, 30)
    .rightSpaceToView(self.view, 30)
    .topSpaceToView(self.confirmPwd, 20)
    .heightIs(45);
    
}

-(void)resetPersonalPwd{
    
    if (![NSString valiMobile:self.accountTF.text]) {
        [self showError:@"手机号格式不正确"];
        return;
    }
    
    if (self.verifyCode.text.length <= 0) {
        [self showError:@"验证码格式不正确"];
        return;
    }
    
    if (self.pwdNew.text.length <= 0) {
        [self showError:@"密码格式不正确"];
        return;
    }
    
    if (![self.pwdNew.text isEqual:self.confirmPwd.text]) {
        [self showError:@"两次输入密码不一致"];
        return;
    }
    
    [self.hud show:YES];
    [BVMineTool reSetPwdMobile:self.accountTF.text newPwd:self.pwdNew.text code:self.verifyCode.text success:^(NSString *status) {
        [self.hud hide:YES];
         [self showError:status];
    } codeFailure:^(NSString *status) {
        [self.hud hide:YES];
        [self showError:status];
    } failure:^(NSError *error) {
        [self.hud hide:YES];
        [self showError:@"网络错误"];
    }];

}

-(void)backBtnClick{
    
    BVLoginViewController *login = [[BVLoginViewController alloc]init];
    [BVKeyWindow addTransitionAnimationWithDuration:0.3f type:TransitionPush subType:FROM_LEFT];
    BVKeyWindow.rootViewController = login;
}

-(UITextField *)textFieldWithText:(NSString *)text{
    
    UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 10, 45)];
    [imgView setImage:[UIImage imageNamed:@"toiuming"]];
    UITextField *textField = [[UITextField alloc]init];
    [self.view addSubview:textField];
    [textField.layer setBorderColor:[UIColor BVBaseOrangeColor].CGColor];
    [textField.layer setBorderWidth:1.0];
    [textField.layer setCornerRadius:5.0];
    [textField setPlaceholder:text];
    [textField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];
    [textField setLeftView:imgView];
    [textField setLeftViewMode:UITextFieldViewModeAlways];
    [textField setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
    return textField;
    
}

-(BVMineTool *)mineTool{
    if (!_mineTool) {
        _mineTool = [[BVMineTool alloc]init];
    }
    return _mineTool;
}

-(void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
